import React, { Component } from 'react';
import './style.css'
import {Button, message} from "antd"
import axios from 'axios'

interface IState{
    isLogin:boolean
}

class Home extends React.Component<any, IState>{
    state={
        isLogin:true,
    }

    async componentDidMount() {
        let res=await axios.get('/api/isLogin')
        if(!res.data?.data){
            this.setState({
                isLogin:false
            })
        }
    }

    handleLogoutClick=async (e:React.MouseEvent)=>{
        let res=await axios.get('/api/logout')
        if(!res.data?.data){
            this.setState({
                isLogin:false
            })
        }
        window.location.href='/login'
    }

    showData= (e:React.MouseEvent)=>{
        window.location.href='/show'
    }

    getData=async (e:React.MouseEvent)=>{
        let res=await axios.get('/api/getData')
        if(res.data?.data){
            message.success('抓取成功')
        }
    }

    render() {
        const {isLogin} = this.state
        if(isLogin) {
            return (
                <div className='home-page'>
                    <Button type="primary" onClick={this.getData}>爬取</Button>
                    <Button type="primary" onClick={this.showData}>展示</Button>
                    <Button type="primary" onClick={this.handleLogoutClick}>退出</Button>
                </div>
            );
        }
        window.location.href='/login'

    }
}

export default Home;